//
//  XVVTextViewWithPlaceholder.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/1.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVVTextViewWithPlaceholder.h"
#import "XXTool.h"

@interface XVVTextViewWithPlaceholder ()

@property (nonatomic) VerticalSettingLabel* placeholderLabel;

@property (nonatomic) UITextView *inputView;

@end

@implementation XVVTextViewWithPlaceholder

-(UITextView*)inputView
{
    if (_inputView==nil)
    {
        _inputView = [[UITextView alloc] init];
        _inputView.backgroundColor = [UIColor clearColor];
        [self addSubview:_inputView];
        
        //textview编辑完成
        UIToolbar * topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30)];
        [topView setBarStyle:UIBarStyleDefault];
        UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"完成", nil) style:UIBarButtonItemStyleDone target:self action:@selector(clickTextFieldDone)];
        [doneButton setTintColor:[XXTool colorWithProject]];
        NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace, doneButton, nil];
        [topView setItems:buttonsArray];
        [_inputView setInputAccessoryView:topView];
    }
    return _inputView;
}

-(VerticalSettingLabel*)placeholderLabel
{
    if (_placeholderLabel==nil)
    {
        _placeholderLabel = [[VerticalSettingLabel alloc] init];
        _placeholderLabel.numberOfLines = 0;
        _placeholderLabel.verticalAlignment = VerticalAlignmentTop;
        _placeholderLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:_placeholderLabel];
        
        
    }
    return _placeholderLabel;
}

-(instancetype)init
{
    self = [super init];
    if (self)
    {
        [self inputView];
        [self placeholderLabel];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

-(void)refreshLayout:(CGRect)frame
{
    _inputView.frame = CGRectMake(11
                                  , 12
                                  ,frame.size.width-15*2
                                  , frame.size.height-20*2);
    
    _placeholderLabel.frame = CGRectMake(15
                                         , 20
                                         ,frame.size.width-15*2
                                         , frame.size.height-20*2);
}

#pragma mark setting
-(void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    _placeholderLabel.text = _placeholder;
}

-(void)setTextFont:(UIFont *)textFont
{
    _textFont = textFont;
    _placeholderLabel.font = _textFont;
    _inputView.font = _textFont;
}

-(void)setPlaceholder_textColor:(UIColor *)placeholder_textColor
{
    _placeholder_textColor = placeholder_textColor;
    _placeholderLabel.textColor = _placeholder_textColor;
}

-(void)textChanged
{
    if(_inputView.text.length>0)
    {
        _placeholderLabel.hidden = YES;
    }
    else if(_inputView.text.length==0)
    {
        _placeholderLabel.hidden = NO;
    }
}

-(void)clickTextFieldDone
{
    [_inputView resignFirstResponder];
}

#pragma mark api
-(NSString*)inputTextString
{
    return _inputView.text;
}

-(void)setinputTextString:(NSString *)text
{
    if (![text isEqualToString:@""])
    {
        _inputView.text = text;
        _placeholderLabel.hidden = YES;
    }
    else
    {
        _inputView.text = @"";
        _placeholderLabel.hidden = NO;
    }
}

@end
